package com.example.umbrella.adapter;

import android.bluetooth.BluetoothGattCharacteristic;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.umbrella.R;
import com.google.android.material.button.MaterialButton;
import com.google.android.material.chip.Chip;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CharacteristicAdapter extends RecyclerView.Adapter<CharacteristicAdapter.ViewHolder> {
    private List<BluetoothGattCharacteristic> characteristics = new ArrayList<>();
    private Map<String, String> characteristicValues = new HashMap<>();
    private OnCharacteristicWriteListener writeListener;

    public interface OnCharacteristicWriteListener {
        void onWrite(BluetoothGattCharacteristic characteristic);
    }

    public void setOnCharacteristicWriteListener(OnCharacteristicWriteListener listener) {
        this.writeListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.characteristic_item, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        BluetoothGattCharacteristic characteristic = characteristics.get(position);
        String uuid = characteristic.getUuid().toString();
        
        // 设置UUID
        holder.characteristicUuid.setText(uuid);
        
        // 检查权限
        int properties = characteristic.getProperties();
        boolean canRead = (properties & BluetoothGattCharacteristic.PROPERTY_READ) != 0;
        boolean canWrite = (properties & (BluetoothGattCharacteristic.PROPERTY_WRITE | 
                          BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE)) != 0;
        
        // 如果既不可读也不可写，隐藏权限标识
        if (!canRead && !canWrite) {
            holder.permissionChip.setVisibility(View.GONE);
        } else {
            holder.permissionChip.setVisibility(View.VISIBLE);
            StringBuilder permissionText = new StringBuilder();
            if (canRead) permissionText.append("可读 ");
            if (canWrite) permissionText.append("可写");
            holder.permissionChip.setText(permissionText);
        }
        
        // 设置可读区域
        if (canRead) {
            holder.readableLayout.setVisibility(View.VISIBLE);
            String value = characteristicValues.get(uuid);
            holder.characteristicValue.setText(value != null ? value : "等待读取...");
        } else {
            holder.readableLayout.setVisibility(View.GONE);
        }
        
        // 设置可写区域
        if (canWrite) {
            holder.writableLayout.setVisibility(View.VISIBLE);
            holder.writeButton.setOnClickListener(v -> {
                if (writeListener != null) {
                    writeListener.onWrite(characteristic);
                }
            });
        } else {
            holder.writableLayout.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return characteristics.size();
    }

    public void updateCharacteristics(List<BluetoothGattCharacteristic> newCharacteristics) {
        characteristics.clear();
        characteristics.addAll(newCharacteristics);
        notifyDataSetChanged();
    }

    public void updateCharacteristicValue(String uuid, String value) {
        // 直接存储ASCII值
        characteristicValues.put(uuid, value);
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView characteristicUuid;
        Chip permissionChip;
        View readableLayout;
        TextView characteristicValue;
        View writableLayout;
        MaterialButton writeButton;

        ViewHolder(View view) {
            super(view);
            characteristicUuid = view.findViewById(R.id.characteristic_uuid);
            permissionChip = view.findViewById(R.id.permission_chip);
            readableLayout = view.findViewById(R.id.readable_layout);
            characteristicValue = view.findViewById(R.id.characteristic_value);
            writableLayout = view.findViewById(R.id.writable_layout);
            writeButton = view.findViewById(R.id.write_button);
        }
    }
} 